﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using JigLibX.Collision;
using JigLibX.Geometry;
using JigLibX.Physics;
using JigLibX.Math;
using JigLibX.Utils;
using Microsoft.Xna.Framework.Graphics;

namespace Library
{
    public class HeightMapObject : PhysicsObject 
    {

        public HeightMapInfo heightMapInfo;

        public HeightMapObject(HeightMapInfo heightMapInfo, Vector2 sh)
        {
            Setup(heightMapInfo, sh);
            InitializeComponent(Engine.DefScreen);
        }

        public HeightMapObject(HeightMapInfo heightMapInfo, Vector2 sh, Screen Parent)
        {
            Setup(heightMapInfo, sh);
            InitializeComponent(Parent);
        }

        void Setup(HeightMapInfo heightMapInfo, Vector2 sh)
        {
            Body = new Body();
            CollisionSkin = new CollisionSkin();
            this.heightMapInfo = heightMapInfo;

            Array2D field = new Array2D(heightMapInfo.Heights.GetUpperBound(0), 
                heightMapInfo.Heights.GetUpperBound(1));

            for (int x = 0; x < heightMapInfo.Heights.GetUpperBound(0); x++)
            {
                for (int z = 0; z < heightMapInfo.Heights.GetUpperBound(1); z++)
                {
                    field.SetAt(x, z, heightMapInfo.Heights[x, z]);
                }
            }

            Body.MoveTo(new Vector3(sh.X, 0, sh.Y), Matrix.Identity);
            CollisionSkin.AddPrimitive(new Heightmap(field, sh.X, sh.Y, 1, 1),
                new MaterialProperties(0.7f, 0.7f, 0.6f));
            PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(CollisionSkin);
        }
    }
}
